<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Utils\ResponseResult\JsonResult;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

class SettingsController extends Controller
{
    /**
     * 获取系统设置
     */
    public function get(): JsonResponse
    {
        $settings = DB::table('settings')->first();
        
        if (!$settings) {
            // 如果没有设置，返回默认值
            return JsonResult::success([
                'sitetitle' => '未设置网站标题',
                'sitelogo' => 'http://localhost:5173/favicon.svg',
                'dashboard_tips_title' => '未设置仪表盘标题',
                'ngx_check' => false,
                'motto' => '未设置系统标语'
            ], '获取系统设置成功');
        }
        
        return JsonResult::success([
            'sitetitle' => empty($settings->sitetitle) ? '未设置网站标题' : $settings->sitetitle,
            'sitelogo' => empty($settings->sitelogo) ? 'http://localhost:5173/favicon.svg' : $settings->sitelogo,
            'dashboard_tips_title' => empty($settings->dashboard_tips_title) ? '未设置仪表盘标题' : $settings->dashboard_tips_title,
            'ngx_check' => $settings->ngx_check ?? false,
            'motto' => empty($settings->motto) ? '未设置系统标语' : $settings->motto
        ], '获取系统设置成功');
    }

    /**
     * 更新系统设置
     */
    public function update(Request $request): JsonResponse
    {
        // 验证请求数据
        $validator = Validator::make($request->all(), [
            'sitetitle' => 'required|string|max:100',
            'sitelogo' => 'required|string|max:255',
            'motto' => 'required|string|max:255'
        ]);

        if ($validator->fails()) {
            return JsonResult::forbidden(null, $validator->errors()->first());
        }

        $sitetitle = $request->input('sitetitle');
        $sitelogo = $request->input('sitelogo');
        $motto = $request->input('motto');

        // 检查是否已存在设置
        $exists = DB::table('settings')->exists();

        if ($exists) {
            // 更新设置
            DB::table('settings')->update([
                'sitetitle' => $sitetitle,
                'sitelogo' => $sitelogo,
                'motto' => $motto,
                'updated_at' => now()
            ]);
        } else {
            // 创建新设置
            DB::table('settings')->insert([
                'sitetitle' => $sitetitle,
                'sitelogo' => $sitelogo,
                'motto' => $motto,
                'created_at' => now(),
                'updated_at' => now()
            ]);
        }

        return JsonResult::success(null, '更新系统设置成功');
    }

    /**
     * 获取仪表盘标题
     */
    public function getDashboardTitle(): JsonResponse
    {
        $settings = DB::table('settings')->first();
        
        if (!$settings) {
            return JsonResult::success([
                'dashboard_tips_title' => '欢迎使用后台管理系统'
            ],"获取仪表盘标题成功");
        }
        
        return JsonResult::success([
            'dashboard_tips_title' => $settings->dashboard_tips_title ?? '欢迎使用后台管理系统'
        ],"获取仪表盘标题成功");
    }

    /**
     * 更新仪表盘标题
     */
    public function updateDashboardTitle(Request $request): JsonResponse
    {
        // 验证请求数据
        $validator = Validator::make($request->all(), [
            'dashboard_tips_title' => 'required|string|max:100'
        ]);

        if ($validator->fails()) {
            return JsonResult::forbidden(null, $validator->errors()->first());
        }

        $dashboard_tips_title = $request->input('dashboard_tips_title');

        // 检查是否已存在设置
        $exists = DB::table('settings')->exists();

        if ($exists) {
            // 更新设置
            DB::table('settings')->update([
                'dashboard_tips_title' => $dashboard_tips_title,
                'updated_at' => now()
            ]);
        } else {
            // 创建新设置
            DB::table('settings')->insert([
                'dashboard_tips_title' => $dashboard_tips_title,
                'created_at' => now(),
                'updated_at' => now()
            ]);
        }

        return JsonResult::success(null, '更新仪表盘标题成功');
    }

    /**
     * 获取nginx检查状态
     */
    public function getNginxCheck(): JsonResponse
    {
        $settings = DB::table('settings')->first();
        
        if (!$settings) {
            return JsonResult::success([
                'ngx_check' => false
            ], "获取nginx检查链接成功");
        }
        
        return JsonResult::success([
            'ngx_check' => $settings->ngx_check ?? false
        ], "获取nginx检查链接成功");
    }
}